DATA SEGMENT
    str     DB 20 DUP(2), 19 DUP(1), 0,3
    substr  DB 19 DUP(1), 0 
    found   DB 0
    pos     DW 0
    len     DW 0    ; the length of substr
DATA ENDS

STACK SEGMENT STACK
    DW 100 DUP(?)
STACK ENDS      

CODE SEGMENT
    ASSUME CS:CODE, DS:DATA, SS:STACK
START:
    MOV AX, DATA
    MOV DS, AX
    
    XOR BX, BX      ; the main pointer
    MOV found, BL   ; initialize
    ; the outer loop
LO: CMP str[BX], 0       
    JZ  OT          ; goto out if traversal is done
    MOV AL, substr
    CMP str[BX], AL
    JNZ NO
    CALL LI         ; goto the inner loop 
NO: INC BX
    JMP LO  
    
OT: MOV AH, 4CH     ; DONE
    INT 21H
    
LI  PROC NEAR
    MOV pos, BX
    MOV SI, 1       ; the offset pointer
    ; to compare str[BX+SI] and substr[SI]
CP: CMP substr[SI], 0
    JZ  EQ          ; goto equal if traversal is done
    CMP substr[SI+1], 0
    JNZ CB          ; compare a single byte
    DEC SI 
CB: MOV AX, WORD PTR substr[SI]
    CMP WORD PTR str[BX+SI], AX
    JNZ BK
    INC SI
    INC SI
    JMP CP
BK: RET
LI  ENDP
    
EQ: MOV found, 0FFH
    MOV len, SI
    JMP OT

CODE ENDS
END START
